/**create by framework at 2019年05月30日 15:47:03**/
package com.star.truffle.module.fzjz.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.star.truffle.core.StarServiceException;
import com.star.truffle.core.jdbc.Page;
import com.star.truffle.core.jdbc.Page.OrderType;
import com.star.truffle.core.web.ApiCode;
import com.star.truffle.core.web.ApiResult;
import com.star.truffle.module.fzjz.domain.WarCategory;
import com.star.truffle.module.fzjz.dto.req.WarCategoryRequestDto;
import com.star.truffle.module.fzjz.dto.res.WarCategoryResponseDto;
import com.star.truffle.module.fzjz.service.WarCategoryService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/warCategory")
public class WarCategoryController {

  @Autowired
  private WarCategoryService warCategoryService;

  @ResponseBody
  @RequestMapping(value = "/all", method = {RequestMethod.POST, RequestMethod.GET})
  public List<WarCategoryResponseDto> resources(WarCategoryRequestDto warCategoryRequestDto) {
    List<WarCategoryResponseDto> resources;
    if (null == warCategoryRequestDto.getParentId()) {
      WarCategoryResponseDto dto = new WarCategoryResponseDto();
      dto.setCateId(0L);
      dto.setName("作战图");
      dto.setChildNum(1);
      resources = Arrays.asList(dto);
    }else {
      warCategoryRequestDto.setDisabledCate(1);
      resources = warCategoryService.queryWarCategory(warCategoryRequestDto);
    }
    return resources;
  }
  
  @RequestMapping(value = "/lists", method = RequestMethod.GET)
  public String list() {
    return "mgr/warCategory/list";
  }

  @RequestMapping(value = "/editBefore/{cateId}", method = RequestMethod.GET)
  public String editBefore(@PathVariable Long cateId, Model model) {
    WarCategoryResponseDto warCategoryResponseDto = this.warCategoryService.getWarCategory(cateId);
    model.addAttribute("warCategoryResponseDto", warCategoryResponseDto);
    return "mgr/warCategory/editWarCategory";
  }

  @ResponseBody
  @RequestMapping(value = "/detail", method = RequestMethod.POST)
  public ApiResult<WarCategoryResponseDto> detail(Long cateId) {
    try {
      WarCategoryResponseDto dto = this.warCategoryService.getWarCategory(cateId);
      return ApiResult.success(dto);
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
  
  @ResponseBody
  @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET})
  public Map<String, Object> list(WarCategoryRequestDto warCategoryRequestDto, Integer page, Integer rows, String sord, String sidx) {
    Page pager = new Page(page, rows, sidx, OrderType.desc.name().equals(sord) ? OrderType.desc : OrderType.asc);
    warCategoryRequestDto.setPager(pager);
    List<WarCategoryResponseDto> list = warCategoryService.queryWarCategory(warCategoryRequestDto);
    Long count = warCategoryService.queryWarCategoryCount(warCategoryRequestDto);

    Map<String, Object> map = new HashMap<>();
    map.put("page", pager.getPageNum());
    map.put("total", count % pager.getPageSize() == 0 ? count / pager.getPageSize() : count / pager.getPageSize() + 1);
    map.put("records", count);
    map.put("rows", list);
    return map;
  }

  @ResponseBody
  @RequestMapping(value = "/add", method = RequestMethod.POST)
  public ApiResult<Long> add(@RequestBody WarCategory warCategory) {
    try {
      Long id = warCategoryService.saveWarCategory(warCategory);
      return ApiResult.success(id);
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
  
  @ResponseBody
  @RequestMapping(value = "/edit", method = RequestMethod.POST)
  public ApiResult<Void> edit(@RequestBody WarCategoryRequestDto warCategoryRequestDto) {
    try {
      warCategoryService.updateWarCategory(warCategoryRequestDto);
      return ApiResult.success();
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
  
  @ResponseBody
  @RequestMapping(value = "/updateIdx", method = RequestMethod.POST)
  public ApiResult<Void> updateIdx(Long cateId, Long brotherId, String type) {
    try {
      warCategoryService.updateIdx(cateId, brotherId, type);
      return ApiResult.success();
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }

  @ResponseBody
  @RequestMapping(value = "/updateIdx2", method = RequestMethod.POST)
  public ApiResult<Void> updateIdx2(Long cateId, Long targetId, String type) {
    try {
      warCategoryService.updateIdx2(cateId, targetId, type);
      return ApiResult.success();
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }

  @ResponseBody
  @RequestMapping(value = "/deleted", method = RequestMethod.POST)
  public ApiResult<Void> delete(String ids) {
    try {
      warCategoryService.deleteWarCategory(ids);
      return ApiResult.success();
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
  
  @ResponseBody
  @RequestMapping(value = "/disabled", method = RequestMethod.POST)
  public ApiResult<Void> disabled(@RequestParam String ids) {
    ApiResult<Void> apiResult = null;
    try {
      this.warCategoryService.disabledCategory(ids);
      apiResult = ApiResult.success();
    } catch (StarServiceException e) {
      e.printStackTrace();
      apiResult = ApiResult.fail(e.getCode(), e.getMessage());
    } catch (Exception e) {
      e.printStackTrace();
      apiResult = ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
    return apiResult;
  }
  
  @ResponseBody
  @RequestMapping(value = "/enabled", method = RequestMethod.POST)
  public ApiResult<Void> enabled(@RequestParam String ids) {
    ApiResult<Void> apiResult = null;
    try {
      this.warCategoryService.enabledCategory(ids);
      apiResult = ApiResult.success();
    } catch (StarServiceException e) {
      e.printStackTrace();
      apiResult = ApiResult.fail(e.getCode(), e.getMessage());
    } catch (Exception e) {
      e.printStackTrace();
      apiResult = ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
    return apiResult;
  }

}